package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.SysRole;
import com.itheima.domain.SysUser;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleService roleService;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public void save(SysUser user) {

        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userDao.save(user);
    }

    @Override
    public List<SysUser> findAll() {
        return userDao.findAll();
    }

    @Override
    public Map<String, Object> toAddRolePage(Integer id) {
        List<SysRole> allRoles = roleService.findAll();
        List<Integer> myRoles = userDao.findRolesByUid(id);
        Map<String, Object> map = new HashMap<>();
        map.put("allRoles", allRoles);
        map.put("myRoles", myRoles);
        return map;
    }

    @Override
    public void addRoleToUser(Integer userId, Integer[] ids) {
        userDao.removeRoles(userId);
        if (ids != null) {
            for (Integer rid : ids) {
                userDao.addRoles(userId, rid);
            }
        }

    }


    /**
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {


        try {
            SysUser sysUser = userDao.findByName(username);

            if (ObjectUtils.isEmpty(sysUser)) {

                return null;
            }

            List<SimpleGrantedAuthority> authorities = new ArrayList<>();

            List<SysRole> roles = sysUser.getRoles();

            roles.forEach(x -> {
                authorities.add(new SimpleGrantedAuthority(x.getRoleName()));
            });

            //最终需要返回一个SpringSecurity的UserDetails对象，{noop}表示不加密认证
            //这四个bool值有一个为false就不能登录
            return new User(sysUser.getUsername(), sysUser.getPassword(), sysUser.getStatus() == 1, true, true, true, authorities);

        } catch (Exception e) {

            e.printStackTrace();


        }

        return null;

    }
}
